#include <cstdint>
#include <map>

#include "ecmascript/compiler/assembler/riscv64/assembler_riscv64.h"

#include "ecmascript/base/bit_helper.h"
#include "ecmascript/ecma_macros.h"

namespace panda::ecmascript::riscv64 
{
	using namespace panda::ecmascript::base;

	void AssemblerRiscv64::Lui(const Register &rd, const Immediate& imm20)
	{
		uint32_t code = 0;
		code = (imm20.Value() << U_TYPE_imm20_LOWBITS)  | Rd(rd.GetId()) | opCodeULui;
		EmitU32(code);
	}

	void AssemblerRiscv64::Auipc(const Register &rd, const Immediate& imm20)
	{
		uint32_t code = 0;
		code = (imm20.Value() << U_TYPE_imm20_LOWBITS)  | Rd(rd.GetId()) | opCodeUAuipc;
		EmitU32(code);
	}

	void AssemblerRiscv64::Jal(const Register &rd, const Immediate& imm20)
	{
		uint32_t code = 0;
		uint32_t imm =   ((imm20.Value() & 0x80000)         // [20]
						| (imm20.Value() & 0x3FF) << 9 		// [10:1]
						| (imm20.Value() & 0x400) >> 2		// [11]
						| (imm20.Value() & 0x7F800) >> 11) 	// [19:12]
						>> 1;
		code = (imm << J_TYPE_imm20_LOWBITS) | Rd(rd.GetId()) | opCodeJal;
		EmitU32(code);
	}


}